VERSION 5.00
Begin VB.Form BugSystem 
   Caption         =   "Bug System"
   ClientHeight    =   8520
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   11430
   BeginProperty Font 
      Name            =   "MS Serif"
      Size            =   12
      Charset         =   0
      Weight          =   400
      Underline       =   0   'False
      Italic          =   0   'False
      Strikethrough   =   0   'False
   EndProperty
   LinkTopic       =   "Form1"
   MDIChild        =   -1  'True
   ScaleHeight     =   8520
   ScaleWidth      =   11430
   WindowState     =   2  'Maximized
   Begin VB.Frame Frame2 
      Caption         =   "All Bugs"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   8415
      Left            =   9240
      TabIndex        =   1
      Top             =   360
      Width           =   8895
      Begin VB.CommandButton Command1 
         Caption         =   "Bug History"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   495
         Left            =   5760
         TabIndex        =   24
         Top             =   7800
         Width           =   1095
      End
      Begin VB.CommandButton cmdFresh 
         Caption         =   "Refresh"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   495
         Left            =   7200
         TabIndex        =   12
         Top             =   7800
         Width           =   1095
      End
      Begin VB.CommandButton cmdFilter 
         Appearance      =   0  'Flat
         Caption         =   "Filter"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   615
         Left            =   7560
         TabIndex        =   11
         Top             =   480
         Width           =   1095
      End
      Begin VB.ListBox lstBugs 
         ForeColor       =   &H00000080&
         Height          =   5760
         Left            =   240
         TabIndex        =   10
         Top             =   1560
         Width           =   8415
      End
      Begin VB.TextBox txtKeyword 
         Appearance      =   0  'Flat
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   375
         Left            =   4800
         TabIndex        =   9
         Text            =   "Text1"
         Top             =   840
         Width           =   2535
      End
      Begin VB.ComboBox Combo3 
         Appearance      =   0  'Flat
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   315
         Left            =   4800
         TabIndex        =   7
         Text            =   "Combo1"
         Top             =   480
         Width           =   1455
      End
      Begin VB.ComboBox Combo2 
         Appearance      =   0  'Flat
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   315
         Left            =   1440
         TabIndex        =   5
         Text            =   "Combo1"
         Top             =   840
         Width           =   1455
      End
      Begin VB.ComboBox Combo1 
         Appearance      =   0  'Flat
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   315
         Left            =   1440
         TabIndex        =   3
         Text            =   "Combo1"
         Top             =   480
         Width           =   1455
      End
      Begin VB.Label Label4 
         Caption         =   "Key words:"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   375
         Left            =   3720
         TabIndex        =   8
         Top             =   840
         Width           =   1215
      End
      Begin VB.Label Label3 
         Caption         =   "Assign To:"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   375
         Left            =   3720
         TabIndex        =   6
         Top             =   480
         Width           =   1215
      End
      Begin VB.Label Label2 
         Caption         =   "Status:"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   375
         Left            =   720
         TabIndex        =   4
         Top             =   840
         Width           =   855
      End
      Begin VB.Label Label1 
         Caption         =   "Priority:"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   375
         Left            =   720
         TabIndex        =   2
         Top             =   480
         Width           =   855
      End
   End
   Begin VB.Frame Frame1 
      Caption         =   "Report a bug"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   8415
      Left            =   480
      TabIndex        =   0
      Top             =   360
      Width           =   8295
      Begin VB.ComboBox cmbStatusR 
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   315
         Left            =   4680
         TabIndex        =   26
         Top             =   2640
         Width           =   1335
      End
      Begin VB.CommandButton cmdNew 
         Caption         =   "New Bug"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   495
         Left            =   4200
         TabIndex        =   25
         Top             =   7800
         Width           =   1335
      End
      Begin VB.CommandButton cmdSubmit 
         Caption         =   "Submit"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   495
         Left            =   6240
         TabIndex        =   23
         Top             =   7800
         Width           =   1335
      End
      Begin VB.TextBox txtDes 
         Appearance      =   0  'Flat
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   4215
         Left            =   480
         MultiLine       =   -1  'True
         ScrollBars      =   2  'Vertical
         TabIndex        =   22
         Text            =   "BugSystem.frx":0000
         Top             =   3480
         Width           =   7095
      End
      Begin VB.ComboBox cmbAssignTo 
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   315
         Left            =   1560
         TabIndex        =   20
         Top             =   2640
         Width           =   1335
      End
      Begin VB.ComboBox cmbPriority 
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   315
         Left            =   1560
         TabIndex        =   18
         Top             =   2040
         Width           =   1335
      End
      Begin VB.ComboBox cmbVer 
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   315
         Left            =   1560
         TabIndex        =   16
         Top             =   1440
         Width           =   1335
      End
      Begin VB.TextBox txtTitle 
         Appearance      =   0  'Flat
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   495
         Left            =   1560
         TabIndex        =   14
         Top             =   600
         Width           =   4935
      End
      Begin VB.Label Label10 
         Caption         =   "Status:"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   375
         Left            =   3600
         TabIndex        =   27
         Top             =   2640
         Width           =   1215
      End
      Begin VB.Label Label9 
         Caption         =   "Description:"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   375
         Left            =   480
         TabIndex        =   21
         Top             =   3120
         Width           =   1215
      End
      Begin VB.Label Label8 
         Caption         =   "Assign To:"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   375
         Left            =   480
         TabIndex        =   19
         Top             =   2640
         Width           =   1215
      End
      Begin VB.Label Label7 
         Caption         =   "Priority:"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   375
         Left            =   480
         TabIndex        =   17
         Top             =   2040
         Width           =   1215
      End
      Begin VB.Label Label6 
         Caption         =   "Version:"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   375
         Left            =   480
         TabIndex        =   15
         Top             =   1440
         Width           =   1215
      End
      Begin VB.Label Label5 
         Caption         =   "Bug Title:"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   375
         Left            =   480
         TabIndex        =   13
         Top             =   720
         Width           =   1215
      End
   End
End
Attribute VB_Name = "BugSystem"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Const PRO_LOW As Integer = 1
Const PRO_MID As Integer = 2
Const PRO_HIG As Integer = 3
Const PRO_URG As Integer = 4
Const PRO_SER As Integer = 5

Const STA_UNFIXED As Integer = 1
Const STA_FIXED As Integer = 2
Const STA_REOPEN As Integer = 3
Const STA_PENDING As Integer = 4
Const STA_CLOSE As Integer = 5


Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim rsUser As New ADODB.Recordset

Dim priorityArr(0 To 10) As Integer
Dim assignToArr(0 To NUMBER_USERS) As Integer
Dim statusArr(0 To 10) As Integer
Dim IdArr(0 To MAX_BUGS) As Integer

Dim sHistory As String

Dim bUpdate As Boolean

Private Sub cmdFilter_Click()
    cmdFresh_Click
End Sub

Private Sub cmdFresh_Click()
    Call LoadBugs(Combo1.ListIndex, Combo2.ListIndex, Combo3.ListIndex, Trim(txtKeyword.Text))
End Sub

Private Sub cmdNew_Click()
    txtTitle.Enabled = True
    txtTitle.Text = ""
    cmbPriority.ListIndex = -1
    cmbAssignTo.ListIndex = -1
    txtDes.Text = ""
    txtTitle.SetFocus
    sHistory = ""
    'init to not fixed
    cmbStatusR.ListIndex = 0
    cmbStatusR.Enabled = False
    bUpdate = False
End Sub

Private Sub cmdSubmit_Click()
    Dim sql As String
    Dim iPriority As Integer
    Dim iAssignTo, iStatus, id As Integer
    
    If txtTitle.Text = "" Or cmbAssignTo.ListIndex = -1 Or cmbPriority.ListIndex = -1 Then
        MsgBox "Please check see is there anything you left blank?", vbOKOnly, SOFT_TITLE
    Else
        
       ' sql = "SET NAMES gbk"
       ' Call CloseRecordSet(rs)
        'On Error Resume Next
        'OperateDatabase rs, cn, sql
        
        iPriority = priorityArr(cmbPriority.ListIndex)
        iAssignTo = assignToArr(cmbAssignTo.ListIndex)
        iStatus = statusArr(cmbStatusR.ListIndex)
        'sHistory = sHistory & "-------------------------------------------"
        sHistory = Operator & " assign a bug to " & cmbAssignTo.Text & ". Priority is " & cmbPriority.Text & vbCrLf
       
        If Not bUpdate Then
            'INSERT INTO `BugSys`(`ID`, `Title`, `AssignTo`, `Status`, `Priority`, `Description`, `Reporter`, `ReporterEmail`, `History`) VALUES ([value-1],[value-2],[value-3],[value-4],[value-5],[value-6],[value-7],[value-8],[value-9])
            sql = "INSERT INTO `BugSys`(`Title`, `AssignTo`, `Status`, `Priority`, `Description`, `Reporter`,`History`) VALUES ('" & AddSlashes(Trim(txtTitle.Text)) & _
            "', " & iAssignTo & ", " & iStatus & ", " & iPriority & ", '" & AddSlashes(Trim(txtDes.Text)) & "','" & Operator & _
            "', '" & AddSlashes(sHistory) & "')"
            Call CloseRecordSet(rs)
            OperateDatabase rs, cn, sql
            MsgBox "Bug created!", vbOKOnly, SOFT_TITLE
        Else
            rs.MoveFirst
            id = IdArr(lstBugs.ListIndex)
            sHistory = Operator & " assign a bug to " & cmbAssignTo.Text & ". Priority is " & cmbPriority.Text & vbCrLf & rs.Fields(8).value
            'UPDATE `BugSys` SET `ID`=[value-1],`Title`=[value-2],`AssignTo`=[value-3],`Status`=[value-4],`Priority`=[value-5],`Description`=[value-6],`Reporter`=[value-7],`ReporterEmail`=[value-8],`History`=[value-9] WHERE 1
            sql = "UPDATE `BugSys` SET `AssignTo`=" & iAssignTo & ",`Status`=" & iStatus & ",`Priority`=" & iPriority & ",`Description`='" & AddSlashes(Trim(txtDes.Text)) & "',`Reporter`='" & Operator & "', `History`='" & AddSlashes(sHistory) & "' WHERE ID=" & id
            Call CloseRecordSet(rs)
            OperateDatabase rs, cn, sql
            MsgBox "Bug updated!", vbOKOnly, SOFT_TITLE
        End If
        cmbStatusR.Enabled = False
    End If
    
    bUpdate = False
End Sub

Private Sub Command1_Click()
    Dim sql As String
    Dim id As Integer
    If lstBugs.ListIndex = -1 Then
        MsgBox "Please select one bug which you want to have a look its history!", vbOKOnly, SOFT_TITLE
    Else
        id = IdArr(lstBugs.ListIndex)
        sql = "select History from BugSys where ID=" & id
        Call CloseRecordSet(rs)
        OperateDatabase rs, cn, sql
        
        If Not rs.EOF Then
            MsgBox rs.Fields(0).value
        End If
    End If
End Sub

Private Sub Form_Load()
    Dim sql As String
    
    Call Cn2SQL(cn)
    On Error GoTo openDB
    sql = "SET NAMES gbk"
    Call CloseRecordSet(rs)
    OperateDatabase rs, cn, sql
openDB:
    sql = "select * from BugSys"
    Call CloseRecordSet(rs)
    OperateDatabase rs, cn, sql
    'get all developers and administrators
    sql = "select * from users where acc=3 or acc=5"
    Call CloseRecordSet(rsUser)
    OperateDatabase rsUser, cn, sql
    
    LoadReportABug
    LoadBugReport
    
    bUpdate = False
End Sub

Public Sub LoadReportABug()
    txtTitle.Text = ""
    cmbVer.Enabled = False
    Call ListPriority(cmbPriority)
    Call ListAssignTo(cmbAssignTo)
    Call LoadStatus(cmbStatusR)
    txtDes.Text = ""
    cmbStatusR.ListIndex = 0
    cmbStatusR.Enabled = False
End Sub

Public Function ListPriority(cmb As ComboBox)
    cmb.Clear
    cmb.AddItem "Low"
    priorityArr(0) = 1
    cmb.AddItem "Middle"
    priorityArr(1) = 2
    cmb.AddItem "High"
    priorityArr(2) = 3
    cmb.AddItem "Urgent"
    priorityArr(3) = 4
    cmb.AddItem "Serious"
    priorityArr(4) = 5
    
    priorityArr(5) = 6
End Function


Private Sub CloseRecordSet(ByRef rs As ADODB.Recordset)
On Error GoTo NoErr
        rs.Close
NoErr:
End Sub

Private Sub Form_Unload(Cancel As Integer)
    cn.Close
End Sub

Public Function ListAssignTo(cmb As ComboBox)
    Dim i As Integer
    i = 0
    cmb.Clear
    If Not rsUser.EOF Then
        rsUser.MoveFirst
        While Not rsUser.EOF
            cmb.AddItem rsUser.Fields(4).value
            assignToArr(i) = rsUser.Fields(0).value
            rsUser.MoveNext
            i = i + 1
        Wend
        assignToArr(i) = NUMBER_USERS + 1
        rsUser.MoveFirst
    End If
End Function

Public Sub LoadBugReport()
    Call ListPriority(Combo1)
    Call LoadStatus(Combo2)
    Call ListAssignTo(Combo3)
    Combo1.AddItem "ALL"
    
    Combo2.AddItem "ALL"
    
    Combo3.AddItem "ALL"
    txtKeyword.Text = ""
    Combo2.Text = "ALL"
    Combo1.Text = "ALL"
    Combo3.Text = "ALL"
    
    
    Call LoadBugs(Combo1.ListIndex, Combo2.ListIndex, Combo3.ListIndex, Trim(txtKeyword.Text))
End Sub

Public Sub LoadStatus(ByVal cmb As ComboBox)
    cmb.Clear
    cmb.AddItem "Not fixed"
    statusArr(0) = 1
    cmb.AddItem "Fixed"
    statusArr(1) = 2
    cmb.AddItem "Reopen"
    statusArr(2) = 3
    cmb.AddItem "Pending"
    statusArr(3) = 4
    cmb.AddItem "Close"
    statusArr(4) = 5
    
    statusArr(5) = 6
End Sub


' p-> priority  s-> status to  a-> assign to an ID
Private Function LoadBugs(p As Integer, s As Integer, a As Integer, keyword As String)
    Dim sql, skeyword As String
    Dim bFirst As Boolean
    Dim iIndex As Integer
    bFirst = True
    lstBugs.Clear
    'if all parameters are zeros, that means show all
    sql = "select * from `BugSys`"
    If p <> -1 And p <> Combo1.ListCount - 1 Then
        sql = sql & GetWhereAnd(bFirst) & " `Priority`=" & GetPriority()
    End If
    
    If s <> -1 And s <> Combo2.ListCount - 1 Then
        sql = sql & GetWhereAnd(bFirst) & "`Status`=" & GetStatus()
    End If
    
    If a <> -1 And a <> Combo3.ListCount - 1 Then
        sql = sql & GetWhereAnd(bFirst) & "`AssignTo`=" & GetAssignTo()
    End If
    
    skeyword = Trim(txtKeyword.Text)
    
    If keyword <> "" Then
        sql = sql & GetWhereAnd(bFirst) & "`Title` like '%" & skeyword & "%'"
    End If
        
    Call CloseRecordSet(rs)
    OperateDatabase rs, cn, sql
    iIndex = 1
    While Not rs.EOF
        lstBugs.AddItem "Bug " & iIndex & ": " & rs.Fields(1).value & "   (Reporter: " & rs.Fields(6).value & ")"
        IdArr(iIndex - 1) = rs.Fields(0).value
        iIndex = iIndex + 1
        rs.MoveNext
    Wend
    
End Function

Public Function GetPriority()
    Dim i As Integer
    i = Combo1.ListIndex
    'GetPriority = i + 1
    GetPriority = priorityArr(i)
End Function

Public Function GetStatus()
    Dim i As Integer
    i = Combo2.ListIndex
    GetStatus = i + 1
End Function


Public Function GetAssignTo()
    Dim i As Integer
    i = Combo3.ListIndex
    'GetAssignTo = i + 1
    GetAssignTo = assignToArr(i)
End Function


Private Function GetWhereAnd(ByRef bFirst As Boolean)
    If bFirst Then
        GetWhereAnd = " where "
        bFirst = False
    Else
        GetWhereAnd = " and "
    End If
End Function

Private Sub lstBugs_DblClick()
    Dim sql As String
    Dim id As Integer
    
    id = IdArr(lstBugs.ListIndex)
    sql = "select * from BugSys where ID=" & id
    Call CloseRecordSet(rs)
    OperateDatabase rs, cn, sql
    
    If Not rs.EOF Then
        cmbStatusR.Enabled = True
        
        rs.MoveFirst
        txtTitle.Text = rs.Fields(1).value
        txtTitle.Enabled = False
        cmbPriority.ListIndex = rs.Fields(4).value - 1
        cmbAssignTo.ListIndex = rs.Fields(2).value - 1
        cmbStatusR.ListIndex = rs.Fields(3).value - 1
        txtDes.Text = rs.Fields(5).value
        
        bUpdate = True
    Else
        MsgBox "Fetal Error, can't find this bug in the database! Please contact the administrator.", vbOKOnly, SOFT_TITLE
    End If
End Sub
